﻿// -----------------------------------------------------------------------
// <copyright file="Image.cs">
// Copyright (c) 2013 Andrey Veselov. All rights reserved.
// License:  Microsoft Public License (MS-PL)
// Contacts: http://andrey.moveax.com  andrey@moveax.com
// </copyright>
// -----------------------------------------------------------------------

namespace PortableImagingLibrary
{
    using System.Diagnostics.Contracts;

    public class Image
    {
        private uint[] _pixels;
        private int _width;
        private int _height;

        internal uint[] Pixels
        {
            get
            {
                Contract.Ensures(Contract.Result<uint[]>() != null);
                Contract.Ensures(Contract.Result<uint[]>().Length > 0);

                return this._pixels;
            }
        }

        /// <summary>Gets or sets the color of the background.</summary>
        public uint BackgroundColor { get; set; }

        /// <summary>Gets the width, in pixels, of this Image..</summary>
        public int Width
        {
            get
            {
                Contract.Ensures(Contract.Result<int>() > 0);

                return this._width;
            }
        }

        /// <summary>Gets the height, in pixels, of this Image..</summary>
        public int Height
        {
            get
            {
                Contract.Ensures(Contract.Result<int>() > 0);

                return this._height;
            }
        }

        /// <summary>Initializes a new instance of the <see cref="Image"/> class.</summary>
        public Image()
        {
        }

        /// <summary>Initializes a new instance of the <see cref="Image"/> class. </summary>
        /// <param name="width">The image width.</param>
        /// <param name="height">The image height.</param>
        public Image(int width, int height)
        {
            Contract.Requires(0 < width * height);

            this.Reset(width, height);
        }

        /// <summary>Sets the color of the specified pixel.</summary>
        /// <param name="x">The x-coordinate of the pixel to retrieve.</param>
        /// <param name="y">The x-coordinate of the pixel to retrieve.</param>
        /// <param name="colorARGB">The color to assign to the specified pixel.</param>
        public void SetPixel(int x, int y, uint colorARGB)
        {
            this._pixels[x + y * this._width] = colorARGB;
        }

        /// <summary>Gets the color of the specified pixel.</summary>
        /// <param name="x">The x-coordinate of the pixel to retrieve.</param>
        /// <param name="y">The x-coordinate of the pixel to retrieve.</param>
        /// <returns>Returns the color of the specified pixel.</returns>
        public uint GetPixel(int x, int y)
        {
            return this._pixels[x + y * this._width];
        }

        /// <summary>Clears all image's data and sets specified width and height.</summary>
        /// <param name="width">The new width.</param>
        /// <param name="height">The new height.</param>
        internal void Reset(int width, int height)
        {
            Contract.Requires(0 < width * height);

            this._width = width;
            this._height = height;
            this._pixels = new uint[width * height];

            this.BackgroundColor = 0xffffffff;
        }
    }
}
